package com.software.levelupandroid.taskIssue.view.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.loper7.date_time_picker.DateTimeConfig;
import com.loper7.date_time_picker.dialog.CardDatePickerDialog;
import com.software.levelupandroid.R;
import com.software.levelupandroid.taskIssue.view.activity.TaskIssueActivity;
import com.software.levelupandroid.utils.DateUtil;

import java.util.Calendar;

/**
 * @Description TODO
 * @Author LiangYaJie
 * @CreateTime 2024年05月07日 09:35:11
 */
public class NoticeTaskFragment extends Fragment {
    private TextView tvNoticeTaskTimeSelect;
    private RelativeLayout rlNoticeTaskTimePicker;
    private TaskIssueActivity taskIssueActivity;
    @SuppressLint("MissingInflatedId")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notice_task, container, false);

        tvNoticeTaskTimeSelect = view.findViewById(R.id.tv_notice_task_timeSelect);
        rlNoticeTaskTimePicker = view.findViewById(R.id.rl_notice_task_timePicker);

        if (taskIssueActivity.getTask().getStartTime() != null){
            tvNoticeTaskTimeSelect.setText(taskIssueActivity.getTask().getStartTime());
        }

        rlNoticeTaskTimePicker.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View view) {
                showCardDatePickerDialog();
            }
        });

        return view;
    }

    /**
     * fragment的onAttach()生命周期，在此生命周期中实现Fragment持有对宿主Activity的引用
     * @param context
     */
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof TaskIssueActivity){
            taskIssueActivity = (TaskIssueActivity) context;
        }
    }

    //底部弹出日期时间选择弹窗
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void showCardDatePickerDialog() {

        Calendar calendar = Calendar.getInstance();

        CardDatePickerDialog cardDatePickerDialog = new CardDatePickerDialog.Builder(getContext())
                .setTitle("选择日期")
                .showBackNow(false)
                .setOnChoose("确定", aLong -> {
                    String noticeTime = DateUtil.convertDateSecondToString(aLong, "yyyy年MM月dd日 HH:mm");
                    if (aLong < DateUtil.getNowTimeLong())
                        Toast.makeText(taskIssueActivity, "选择的通知时间已过", Toast.LENGTH_SHORT).show();
                    else {
                        taskIssueActivity.getTask().setStartTime(noticeTime);
                        tvNoticeTaskTimeSelect.setText(noticeTime);
                    }

                    return null;
                })
                .setBackGroundModel(CardDatePickerDialog.CARD)
                .setDisplayType(DateTimeConfig.YEAR, DateTimeConfig.MONTH, DateTimeConfig.DAY, DateTimeConfig.HOUR, DateTimeConfig.MIN)
                .setDefaultTime(calendar.getTimeInMillis())
                .setLabelText("年", "月", "日", "时", "分", "秒")
                .build();
        cardDatePickerDialog.show();
    }

}
